﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using myBoard.Framework.Controls.Navigation;
using myTwitter.CutomControls;
using myTwitter.API;
using System.ComponentModel;
using myBoard.Framework;
using System.Windows.Input;
using System.Collections.ObjectModel;

namespace myTwitter.BO
{
    public class User : IBreadcrumbExplorerEntity
    {
        public long ID { get; set; }
        public string Name { get; set; }
        public string ScreenName { get; set; }
        public string Location { get; set; }
        public string Description { get; set; }
        public string ProfileImageUrl { get; set; }
        public string Url { get; set; }
        public string TwitterUrl { get; set; }
        public bool Protected { get; set; }
        public long FollowersCount { get; set; }
        public Color ProfileBackgroundColor { get; set; }
        public Color ProfileTextColor { get; set; }
        public Color ProfileLinkColor { get; set; }
        public Color ProfileSidebarFillColor { get; set; }
        public Color ProfileSidebarBorderColor { get; set; }
        public int FriendsCount { get; set; }
        public DateTime CreatedAt { get; set; }
        public int FavoriteCount { get; set; }
        public int UTCOffset { get; set; }
        public TimeZone TimeZone { get; set; }
        public string ProfileBackgroundImageUrl { get; set; }
        public bool ProfileBackgroundTile { get; set; }
        public long StatusesCount { get; set; }
        public bool Notifications { get; set; }
        public bool Following { get; set; }
        public Status Status { get; set; }
        public Relationship Relationship { get; set; }

        #region IBreadcrumbExplorerEntity Members

        public System.Windows.Media.Imaging.BitmapImage Icon { get; set; }
        UserInfoControl _uic;

        public System.Windows.FrameworkElement GetControl()
        {
            _uic = new UserInfoControl();
            _uic.SeeUserTweetsRequested += uic_SeeUserTweetsRequested;
            _uic.SeeUserFollowersRequested += _uic_SeeUserFollowersRequested;
            _uic.SeeUserFriendsRequested += _uic_SeeUserFriendsRequested;
            _uic.DataContext = this;
            return _uic;
        }

        void _uic_SeeUserFriendsRequested(User user)
        {
            if (IBreadcrumbExplorerNextEntitySelected != null)
            {
                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += (a, b) =>
                {
                    List<User> u = GlobalController.Instances.ITwitterProvider.GetFriends(user.ScreenName, "-1");
                    b.Result = u;
                };
                bw.RunWorkerCompleted += (a, b) =>
                {
                    Mouse.OverrideCursor = null;
                    _uic.LoadingOff();
                    if (b.Error != null)
                    {
                        ExceptionsManager.LogException("Sinapse", b.Error, true);
                    }
                    else
                    {
                        Users u = new Users(b.Result as List<User>);
                        ((IBreadcrumbExplorerEntity)u).Name = string.Format("{0}'s friends", user.Name);
                        IBreadcrumbExplorerNextEntitySelected(this, u);
                    }
                };
                bw.RunWorkerAsync();
                Mouse.OverrideCursor = Cursors.Wait;
                _uic.LoadingOn();
            }
        }

        void _uic_SeeUserFollowersRequested(User user)
        {
            if (IBreadcrumbExplorerNextEntitySelected != null)
            {
                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += (a, b) =>
                {
                    List<User> u = GlobalController.Instances.ITwitterProvider.GetFollowers(user.ScreenName, "-1");
                    b.Result = u;
                };
                bw.RunWorkerCompleted += (a, b) =>
                {
                    Mouse.OverrideCursor = null;
                    _uic.LoadingOff();
                    if (b.Error != null)
                    {
                        ExceptionsManager.LogException("Sinapse", b.Error, true);
                    }
                    else
                    {
                        Users u = new Users(b.Result as List<User>);
                        ((IBreadcrumbExplorerEntity)u).Name = string.Format("{0}'s followers", user.Name);
                        IBreadcrumbExplorerNextEntitySelected(this, u);
                    }
                };
                bw.RunWorkerAsync();
                Mouse.OverrideCursor = Cursors.Wait;
                _uic.LoadingOn();
            }
        }

        void uic_SeeUserTweetsRequested(User user)
        {
            if (IBreadcrumbExplorerNextEntitySelected != null)
            {
                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += (a, b) =>
                {
                    Tweets t = GlobalController.Instances.ITwitterProvider.GetUserTweets(user);
                    b.Result = t;
                };
                bw.RunWorkerCompleted += (a, b) =>
                {
                    Mouse.OverrideCursor = null;
                    _uic.LoadingOff();
                    if (b.Error != null)
                    {
                        ExceptionsManager.LogException("Sinapse", b.Error, true);
                    }
                    else
                    {
                        Tweets t = b.Result as Tweets;
                        ((IBreadcrumbExplorerEntity)t).Name = string.Format("{0}'s tweets", user.Name);
                        IBreadcrumbExplorerNextEntitySelected(this, t);
                    }
                };
                bw.RunWorkerAsync();
                Mouse.OverrideCursor = Cursors.Wait;
                _uic.LoadingOn();
            }
        }

        public void Dispose()
        {
            _uic.SeeUserTweetsRequested -= uic_SeeUserTweetsRequested;
            _uic.SeeUserFollowersRequested -= _uic_SeeUserFollowersRequested;
            _uic.SeeUserFriendsRequested -= _uic_SeeUserFriendsRequested;
        }

        public void Update(object newValue)
        {
            _uic.DataContext = newValue;
        }

        public event IBreadcrumbExplorerNextEntitySelectedHandler IBreadcrumbExplorerNextEntitySelected;

        #endregion
    }
}
